/**
 * 
 */
package com.imonsoft.common.html;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * HTML select JavaBean
 * 
 * @author LiQianHui
 * @date 2013-04-17
 * @version 2013
 */
public class Select implements Serializable {

	private static final long serialVersionUID = 5976516001635542774L;

	private String id;// HTML property:id
	private String name;// HTML property:name
	private String clazz;// HTML property:class
	private String style;// HTML property:style
	private String mode;// 1:请选择;2:不限
	private String noSelTxt;// 如不为空，则mode无效
	private boolean disabled = false;
	private List<Option> options = new ArrayList<Option>();

	public Select() {
		// TODO Auto-generated constructor stub
	}

	public Select(String name) {
		this.name = name != null ? name : "";
		this.id = this.name;
		this.clazz = "field select";
		this.style = "";
		this.mode = "1";
	}

	public Select(String id, String name, String clazz, String style, String mode, String noSelTxt) {
		this.id = id != null ? id : (name != null ? name : "");
		this.name = name != null ? name : (id != null ? id : "");
		this.clazz = clazz != null ? clazz : "field select";
		this.style = style != null ? style : "";
		this.mode = noSelTxt != null ? null : (mode != null ? mode : "1");
		this.noSelTxt = noSelTxt != null ? noSelTxt : null;
	}

	public Select addOption(String value, String text, boolean selected) {
		options.add(new Option(value, text, selected));
		return this;
	}

	@Override
	public String toString() {
		String selectHtml = "<select id='" + this.id + "' name='" + this.name + "' class='"
				+ this.clazz + "' style='" + this.style + "' " + (disabled ? "disabled" : "") + ">";
		if (noSelTxt != null) {
			selectHtml += "<option value=''>" + noSelTxt + "</option>";
		} else if ("1".equals(mode)) {
			selectHtml += "<option value=''>请选择</option>";
		} else {
			selectHtml += "<option value=''>不限</option>";
		}
		for (Option option : options) {
			selectHtml += option;
		}
		selectHtml += "</select>";
		return selectHtml;
	}

	public class Option {
		private String value;
		private String text;
		private boolean selected;

		public Option() {
			// TODO Auto-generated constructor stub
		}

		public Option(String value, String text, boolean selected) {
			super();
			this.value = value;
			this.text = text;
			this.selected = selected;
		}

		/**
		 * @return the value
		 */
		public String getValue() {
			return value;
		}

		/**
		 * @param value
		 *            the value to set
		 */
		public void setValue(String value) {
			this.value = value;
		}

		/**
		 * @return the text
		 */
		public String getText() {
			return text;
		}

		/**
		 * @param text
		 *            the text to set
		 */
		public void setText(String text) {
			this.text = text;
		}

		@Override
		public String toString() {
			if (selected) {
				return "<option value='" + this.value + "' selected>" + this.text + "</option>";
			} else {
				return "<option value='" + this.value + "'>" + this.text + "</option>";
			}
		}
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the style
	 */
	public String getStyle() {
		return style;
	}

	/**
	 * @param style
	 *            the style to set
	 */
	public void setStyle(String style) {
		this.style = style;
	}

	/**
	 * @return the clazz
	 */
	public String getClazz() {
		return clazz;
	}

	/**
	 * @param clazz
	 *            the clazz to set
	 */
	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	/**
	 * @return the mode
	 */
	public String getMode() {
		return mode;
	}

	/**
	 * @param mode
	 *            the mode to set
	 */
	public void setMode(String mode) {
		this.mode = mode;
	}

	/**
	 * @return the noSelTxt
	 */
	public String getNoSelTxt() {
		return noSelTxt;
	}

	/**
	 * @param noSelTxt
	 *            the noSelTxt to set
	 */
	public void setNoSelTxt(String noSelTxt) {
		this.noSelTxt = noSelTxt;
	}

	/**
	 * @return the disabled
	 */
	public boolean isDisabled() {
		return disabled;
	}

	/**
	 * @param disabled
	 *            the disabled to set
	 */
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public static void main(String[] args) {
		Select select = new Select("id", "name", null, null, null, null);
		select.addOption("1", "苹果", false).addOption("2", "梨子", true);
		System.out.println(select);
	}
}